Getting hold of the wards in Leeds seems tricky, and requires joining two datasets and some HTTP work. Makes it fun, I guess.
We start from data from Leeds city council. But this only gives links to the shapefiles.
Wards in Leeds, (c) Leeds City Council, 2014, https://datamillnorth.org/dataset/wards-in-leeds. This information is licensed under the terms of the Open Government Licence.
In [1]:
import csv, requests
url = "https://aql.datapress.com/leeds/dataset/wards-in-leeds/wardsgeoarea.csv"
response = requests.get(url)
In [2]:
import io
file = io.StringIO(response.content.decode("utf8"))
In [3]:
reader = csv.reader(file)
next(reader)
Out[3]:
In [4]:
import collections
Ward = collections.namedtuple("Ward", ["code", "name", "url"])
wards = [Ward(*row) for row in reader]
wards[0]
Out[4]:
In [5]:
import json
url = "http://statistics.data.gov.uk/boundaries/{}.json"
shapes = dict()
for ward in wards:
response = requests.get(url.format(ward.code))
shapes[ward.code] = json.loads(response.content.decode("utf8"))
In [6]:
import geopandas as gpd
In [7]:
frame = gpd.GeoDataFrame.from_features(shapes.values())
frame.head()
Out[7]:
In [8]:
%matplotlib inline
import matplotlib.pyplot as plt
frame.plot()
Out[8]:
In [9]:
# Set projection as lon/lat, then reproject to "web mercator"
frame.crs = {"init": "EPSG:4326"}
frame = frame.to_crs({"init": "EPSG:3857"})
In [10]:
import tilemapbase
In [12]:
fig, ax = plt.subplots(figsize=(10,10))
extent = tilemapbase.extent_from_frame(frame, buffer=5)
plotter = tilemapbase.Plotter(extent, tilemapbase.tiles.OSM, width=600)
plotter.plot(ax)
frame.plot(ax=ax, column="WD15NM", categorical=True, alpha=0.2)
None
We could add a legend, but geoPandas isn't great at doing that (yet...)
In [ ]: